/*
 * File         : cym_net.c
 * Version      : 0.1.0
 * Author       : Vincent Cheung
 * Date         : Oct. 23, 2013
 * Description  : All the operation for net(CAN, LIN and so on).
 *
 * Copyright (C) 2013, CYM Co., Ltd.
 *
 * Change Logs  :
 * Date			Author		Notes
 * Oct. 23, 2013	Vincent		first version
 *
 */

#include "cym_system.h"

#ifdef CYM_USING_NET

/**
 * @addtogroup Net
 */

/*@{*/

/**
 * This function is used to initialize a net device.
 *
 * @param net_dev the net device to be initilized
 * @param name the name of net device
 * @param flags the flag of net device
 *
 * @return return CYM_EOK when successfully, otherwise return -CYM_ERROR
 */
serr_t cym_net_init(struct net_device *net_dev, const char *name, uint16_t flags)
{
	/* the net device can not be NULL */
	if (net_dev == CYM_NULL)
	{
		return -CYM_ERROR;
	}

	/* register to system device manager */
	cym_device_register(&(net_dev->parent), name, flags);

	return CYM_EOK;
}

/**
 * This function is used to receive a net package from device.
 *
 * @param dev pointer to device structure
 *
 * @return return net package when successfully, otherwise return CYM_NULL
 */
struct net_package *cym_net_rx(p_device_t dev)
{
	p_net_device_t net_rx_device;
	/* net package */
	struct net_package *pack = CYM_NULL;
	
	/* the device can not be NULL */
	if (dev == CYM_NULL)
	{
		return CYM_NULL;
	}

	/* get net device from device */
	net_rx_device = (p_net_device_t)dev;

	if (net_rx_device->net_rx == CYM_NULL)
	{
		return CYM_NULL;
	}

	/* call net device net_rx interface */
	pack = net_rx_device->net_rx(dev);

	/* return the net package from device */
	return pack; 
}

/**
 * This function is used to transmit a net package to device.
 *
 * @param dev pointer to device structure
 * @param pack the net package to be transmitted
 *
 * @return return CYM_EOK when successfully, otherwise return -CYM_ERROR
 */
serr_t cym_net_tx(p_device_t dev, struct net_package *pack)
{
	p_net_device_t net_tx_device;
	serr_t result = -CYM_ERROR;

	/* the device can not be NULL */
	if (dev == CYM_NULL)
	{
		return -CYM_ERROR;
	}

	net_tx_device = (p_net_device_t)dev;

	/* call net device net_tx interface */
	if (net_tx_device->net_tx != CYM_NULL)
	{
		result = net_tx_device->net_tx(dev, pack);	
	}

	return result;
}

#endif	/* CYM_USING_NET */

/*@}*/